package com.gxp.web.controller;

import com.gxp.common.annotation.Audit;
import com.gxp.common.core.controller.BaseController;
import com.gxp.common.core.domain.AjaxResult;
import com.gxp.common.core.domain.entity.ZkServiceType;
import com.gxp.common.core.page.TableDataInfo;
import com.gxp.common.enums.BusinessAction;
import com.gxp.common.enums.BusinessTitle;
import com.gxp.common.utils.DateUtils;
import com.gxp.system.domain.ZkInstrument;
import com.gxp.system.domain.ZknjService;
import com.gxp.system.service.IZkInstrumentService;
import com.gxp.system.service.IZkServiceTypeService;
import com.gxp.system.service.IZknjServiceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务2Controller
 * 
 * @author zh
 * @date 2024-07-18
 */
@Controller
@RequestMapping("/system/service2")
public class ZknjServiceController extends BaseController
{
    private String prefix = "system/project/service2";

    @Autowired
    private IZknjServiceService zknjServiceService;
    @Autowired
    private IZkInstrumentService instrumentService;
    @Autowired
    private IZkServiceTypeService zkServiceTypeService;

    @RequiresPermissions("system:service2:view")
    @GetMapping()
    public String service2()
    {
        return prefix + "/service2";
    }

    /**
     * 查询服务2列表
     */
    @RequiresPermissions("system:service2:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZknjService zknjService)
    {
        startPage();
        List<ZknjService> list = zknjServiceService.selectZknjServices(zknjService);
        return getDataTable(list);
    }

    /**
     * 新增服务2
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<ZkServiceType> zkServiceTypes = zkServiceTypeService.selectSvtypeAll();
        List<ZkInstrument> instruments = instrumentService.selectZkInstrumentList(new ZkInstrument());

        mmap.put("instruments", instruments);
        mmap.put("zkServiceTypes",zkServiceTypes);
        return prefix + "/add";
    }

    /**
     * 新增保存服务2
     */
    @RequiresPermissions("system:service2:add")
    @Audit(businessTitle = BusinessTitle.SERVICE, businessAction = BusinessAction.CREATED, description = "新增服务")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZknjService zknjService)
    {
        zknjService.setCreateBy(getLoginName());
        int row = zknjServiceService.insertZknjService(zknjService);
        return toAjax(row, zknjService.getSerId());
    }

    /**
     * 修改服务2
     */
    @RequiresPermissions("system:service2:edit")
    @GetMapping("/edit/{serId}")
    public String edit(@PathVariable("serId") Long serId, ModelMap mmap)
    {
        List<ZkServiceType> zkServiceTypes = zkServiceTypeService.selectSvtypeAll();
        List<ZkInstrument> instruments = instrumentService.selectZkInstrumentList(new ZkInstrument());
        ZknjService zknjService = zknjServiceService.selectZknjServiceBySerId(serId);
        mmap.put("instruments", instruments);
        mmap.put("zkServiceTypes",zkServiceTypes);
        mmap.put("zknjService", zknjService);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务2
     */
    @RequiresPermissions("system:service2:edit")
    @Audit(businessTitle = BusinessTitle.SERVICE, businessAction = BusinessAction.UPDATED, description = "修改服务")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZknjService zknjService)
    {
        zknjService.setUpdateBy(getLoginName());
        return toAjax(zknjServiceService.updateZknjService(zknjService), zknjService.getSerId());
    }

    /**
     * 删除服务2
     */
    @RequiresPermissions("system:service2:remove")
    @Audit(businessTitle = BusinessTitle.SERVICE, businessAction = BusinessAction.DELETED, description = "删除服务")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zknjServiceService.deleteZknjServiceBySerIds(ids));
    }

    /**
     * 停用/启用服务
     */
    @RequiresPermissions("system:service2:edit")
    @Audit(businessTitle = BusinessTitle.SERVICE, businessAction = BusinessAction.UPDATED, description = "服务状态改变")
    @PostMapping("changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ZknjService zknjService){
        return toAjax(zknjServiceService.changeStatus(zknjService), zknjService.getSerId());
    }

    @RequiresPermissions("system:service2:list")
    @GetMapping("/methods/{serId}")
    public String services(@PathVariable("serId") Long serId, ModelMap mmap){
        mmap.put("serId", serId);
        return "system/project/method/method";
    }


    @RequiresPermissions("system:service2:approve")
    @GetMapping("/approve")
    public String approve(ZknjService service, ModelMap mmap){
        ZknjService zknjService = zknjServiceService.selectZknjServiceBySerId(service.getSerId());
        mmap.addAttribute("zknjService", zknjService);
        return prefix + "/approve";
    }

    @RequiresPermissions("system:service2:approve")
    @Audit(businessTitle = BusinessTitle.SERVICE, businessAction = BusinessAction.APPROVED, description = "服务审核")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(ZknjService service){
        service.setApproveBy(getLoginName());
        service.setApproveTime(DateUtils.getNowDate());
        return toAjax(zknjServiceService.approve(service), service.getSerId());
    }
}
